freeslot("MT_SWITCH_BASE", "MT_SWITCH_BALL")

addHook("MobjSpawn", function(mo)
local target = P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_SWITCH_BALL)
mo.tracer = target
target.tracer = mo
mo.scale = FRACUNIT/2
end, MT_SWITCH_BASE)

addHook("MobjThinker", function(mo)
	if mo
	and mo.valid
	and mo.type == MT_SWITCH_BASE
	and mo.health
	mo.flags2 = $1|MF2_SPLAT
	mo.renderflags = $1|(RF_NOSPLATBILLBOARD|RF_OBJECTSLOPESPLAT)
		if (mo.eflags & MFE_JUSTHITFLOOR)
		mo.momx = 0
		mo.momy = 0
		end
		if mo.tag_lever == nil
		mo.tag_lever = false
		end
		if mo.touched_player == nil
		mo.touched_player = 0
		end
		if mo.tag_number == nil
			if mo.spawnpoint
				if mo.spawnpoint.angle
				mo.tag_number = mo.spawnpoint.angle
				end
			end
		end
		if mo.tag_lever == true
			if mo.touched_player
			and mo.touched_player.valid
			local player = mo.touched_player
			P_LinedefExecute(mo.tag_number, player)
			mo.tag_lever = false
			mo.touched_player = 0
			end
		end
		if mo.tracer
		and mo.tracer.valid
			if mo.tracer.activated == false
				if (mo.eflags & MFE_VERTICALFLIP)
				or (mo.flags2 & MF2_OBJECTFLIP)
				P_SetOrigin(mo.tracer, mo.x, mo.y, mo.z-48*FRACUNIT)
				//mo.tracer.spriteyoffset = -24*FRACUNIT
				//mo.tracer.eflags = $1|MFE_VERTICALFLIP
				else
				P_SetOrigin(mo.tracer, mo.x, mo.y, mo.z+16*FRACUNIT)
				end
			elseif mo.tracer.activated == true
			local gotox = mo.x+FixedMul(cos(mo.tracer.baseangle), mo.tracer.touchspeed)
			local gotoy = mo.y+FixedMul(sin(mo.tracer.baseangle), mo.tracer.touchspeed)
			local angle = R_PointToAngle2(mo.tracer.x, mo.tracer.y, mo.x, mo.y)
			local angle2 = R_PointToAngle2(mo.tracer.x, mo.tracer.y, gotox, gotoy)
			local dist = R_PointToDist2(mo.tracer.x, mo.tracer.y, mo.x, mo.y)
			local dist2 = R_PointToDist2(mo.tracer.x, mo.tracer.y, gotox, gotoy)
				if mo.tracer.launchball == 0
					if dist2 <= 16*FRACUNIT
					P_InstaThrust(mo.tracer, angle2, dist2/2)
					else
					P_InstaThrust(mo.tracer, angle2, dist2/4)
					end
					if dist2 <= FRACUNIT
					mo.tracer.baseangle = $1 + ANGLE_180
						if mo.tracer.touchspeed > 0
						mo.tracer.touchspeed = $1 - 4*FRACUNIT
						end
					end
				end
			end
		end
	end
end, MT_SWITCH_BASE)

addHook("MobjThinker", function(ball)
	if ball
	and ball.valid
	and ball.type == MT_SWITCH_BALL
	and ball.health
		if ball.activated == nil
		ball.activated = false
		elseif ball.activated == false
			if not (ball.state == S_SWITCH_OFF)
			ball.state = S_SWITCH_OFF
			end
		elseif ball.activated == true
			if not (ball.state == S_SWITCH_ON)
			ball.state = S_SWITCH_ON
			end
		end
		if ball.starangle == nil
		ball.starangle = P_RandomRange(0, 360)*ANG1
		else
		ball.starangle = $1 + ANG2
			if ball.launchball == 0
			ball.starangle = $1 + FixedAngle(ball.touchspeed)
			end
		end
		if ball.star == nil
		ball.star = P_SpawnMobj(ball.x, ball.y, ball.z, MT_THOK)
		ball.star.state = S_BALLSTAR
		elseif ball.star
		and ball.star.valid
		local gotox = ball.x+ball.momx+FixedMul(cos(ball.starangle), 16*FRACUNIT)
		local gotoy = ball.y+ball.momy+FixedMul(sin(ball.starangle), 16*FRACUNIT)
		ball.star.angle = ball.starangle+ANGLE_90
		//ball.star.z = ball.z+ball.height/4
		P_SetOrigin(ball.star, gotox, gotoy, ball.z+ball.height/2)
		else
		ball.star = nil
		end
		if ball.touchspeed == nil
		ball.touchspeed = 0
		elseif ball.touchspeed < 0
		ball.touchspeed = 0
		end
		if ball.baseangle == nil
		ball.baseangle = 0
		end
		if ball.launchball == nil
		ball.launchball = 0
		elseif ball.launchball > 0
		ball.launchball = $1 - 1
		//elseif ball.launchball <= 0
		//ball.momx = 0
		//ball.momy = 0
		end
	end
end, MT_SWITCH_BALL)

addHook("TouchSpecial", function(ball, mo)
	if ball
	and ball.valid
	and ball.type == MT_SWITCH_BALL
	and ball.health
		if mo
		and mo.valid
		and mo.player
		and mo.player.playerstate == PST_LIVE
			if ball.tracer
			and ball.tracer.valid
				if ball.activated == false
				S_StartSound(ball, sfx_s1cd)
				ball.activated = true
				ball.tracer.tag_lever = true
				ball.tracer.touched_player = mo
				end
			ball.launchball = 2
				if R_PointToDist2(ball.x, ball.y, ball.tracer.x, ball.tracer.y) <= 32*FRACUNIT
				ball.momx = mo.momx
				ball.momy = mo.momy
				ball.touchspeed = R_PointToDist2(ball.x, ball.y, ball.tracer.x, ball.tracer.y)
				else
				ball.momx = 0
				ball.momy = 0
				ball.touchspeed = 32*FRACUNIT
				end
				if mo.player.speed > 0
				ball.baseangle = R_PointToAngle2(0, 0, mo.momx, mo.momy)+ANGLE_180
				end
			end
		end
	end
return true
end, MT_SWITCH_BALL)